package com.zking.frame;

import com.zking.dao.UserDao;
import com.zking.entity.User;
import com.zking.utils.Tool工具;

import javax.swing.*;
import javax.swing.border.MatteBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.Date;
import java.util.List;

public class Retrieve {
    private JLabel textJLabel, userJLabel, passwordJLabel, teJLabel;// 书写标题，书写用户提示，书写密码提示，注册成功提示.
    private JTextField userField, nameField,passwordField;
    private JButton landButton, registerButton;// 登陆注册按钮
    private JPanel jPanel;
    private boolean ab = false, bc = false, cd = false ,df = false;
    private List<User> usList;
    private JButton jButton;
    private int anInt=0;


    public Retrieve(JFrame a) {
        a.dispose();
        JFrame jFrame = Tool工具.getJFrame();
        jFrame.getContentPane().setBackground(Color.white);

        //获取用户表信息
        UserDao userDao = new UserDao();
        usList = userDao.queryStudents();

        jPanel = new JPanel();
        jPanel.setBounds(20, 50, 600, 900);
        jPanel.setLayout(null);
        jPanel.setBorder(new MatteBorder(2, 2, 2, 2, new Color(63, 247, 18)));
        jPanel.setBackground(new Color(133, 250, 104));
        jFrame.add(jPanel, 0);

        // 加入背景
        ImageIcon icon = new ImageIcon(Land.class.getResource("注册背景.jpg"));
        JLabel jLabel = new JLabel(icon);
        jLabel.setBounds(0, 0, 600, 900);
        jPanel.add(jLabel, -1);

        // 书写标题
        textJLabel = new JLabel("找回密码");
        textJLabel.setForeground(new Color(18, 220, 214));
        textJLabel.setFont(new Font("华文琥珀", Font.PLAIN, 40));
        textJLabel.setBounds(200, 120, 400, 100);
        jPanel.add(textJLabel, 0);

        // 书写提示
        userJLabel = new JLabel("用户名：");
        userJLabel.setBounds(130, 250, 150, 50);
        userJLabel.setFont(new Font("黑体", Font.BOLD, 25));
        jPanel.add(userJLabel, 0);

        // 书写用户框和密码框
        userField = new JTextField("");
        userField.setBounds(230, 250, 200, 50);
        userField.setFont(new Font("黑体", Font.BOLD, 20));
        jPanel.add(userField, 0);
        JLabel usjLabel = new JLabel("",0);
        usjLabel.setBounds(230,300, 200, 50);
        usjLabel.setForeground(Color.gray);
        usjLabel.setFont(new Font("黑体", Font.BOLD, 20));
        jPanel.add(usjLabel, 0);
        JLabel jLabel1 = new JLabel("",0);
        jLabel1.setBounds(230, 400, 200, 50);
        jLabel1.setFont(new Font("黑体", Font.BOLD, 20));
        jPanel.add(jLabel1, 0);
        // 设置聚焦失焦事件
        userField.addFocusListener(new FocusListener() {

            @Override
            public void focusLost(FocusEvent e) {
                // TODO Auto-generated method
                nameField.setText("");
                bc = false;
                jLabel1.setText("请输入你的电话号码");
                if (userField.getText().equals("")) {
                    usjLabel.setText("请输入用户名");
                    nameField.setText("");
                    Tool工具 tool工具 = new Tool工具();
                    tool工具.setSendSms(false);
                    usjLabel.setForeground(Color.gray);
                    ab = false;
                } else {
                    for (int i = 0; i < usList.size(); i++) {
                        if (userField.getText().equals(usList.get(i).getUserName())) {
                            usjLabel.setText("用户名存在");
                            usjLabel.setForeground(Color.green);
                            ab = true;
                            return;
                        }else {
                            ab = false;
                        }
                    }
                    if (!ab){
                        usjLabel.setText("用户名不存在");
                        nameField.setText("");
                        bc = false;
                        usjLabel.setForeground(Color.gray);
                    }
                }
            }

            @Override
            public void focusGained(FocusEvent e) {
                // TODO Auto-generated method stub
                if (usjLabel.getText().equals("请输入用户名") || usjLabel.getText().equals("用户名不存在")) {
                    userField.setText("");
                    userField.setForeground(Color.black);
                }
            }
        });

        passwordJLabel = new JLabel("电话号码：");
        passwordJLabel.setBounds(110, 350, 150, 50);
        passwordJLabel.setFont(new Font("黑体", Font.BOLD, 25));
        jPanel.add(passwordJLabel, 0);

        nameField = new JTextField("");
        nameField.setBounds(230, 350, 200, 50);
        nameField.setForeground(Color.gray);
        nameField.setFont(new Font("黑体", Font.BOLD, 20));
        jPanel.add(nameField, 0);
        // 设置聚焦失焦事件
        nameField.addFocusListener(new FocusListener() {

            @Override
            public void focusLost(FocusEvent e) {
                // TODO Auto-generated method stub
                if (!userField.getText().equals("")&&!userField.getText().equals("请输入用户名")&&!userField.getText().equals("用户名不存在")){
                    System.out.println(userField.getText());
                    if (nameField.getText().equals("")) {
                        jLabel1.setText("请输入你的电话号码");
                        jLabel1.setForeground(Color.gray);
                        bc = false;
                    } else {
                        for (int i = 0; i < usList.size(); i++) {
                            if (userField.getText().equals(usList.get(i).getUserName())) {
                                if (nameField.getText().equals(usList.get(i).getPhoneNumber())){
                                    jLabel1.setText("正确");
                                    jLabel1.setForeground(Color.green);
                                    bc = true;
                                    return;
                                }else {
                                    jLabel1.setText("电话号码不匹配");
                                    jLabel1.setForeground(Color.red);
                                    bc = false;
                                }
                            }else {
                                bc = false;
                            }
                        }
                    }
                }else{
                    jLabel1.setText("请输入用户名");
                    jLabel1.setForeground(Color.red);
                }
            }

            @Override
            public void focusGained(FocusEvent e) {
                // TODO Auto-generated method stub
                if (jLabel1.getText().equals("请输入用户名")||jLabel1.getText().equals("电话号码不匹配")){
                    nameField.setText("");
                    jLabel1.setText("请输入你的电话号码");
                    jLabel1.setForeground(Color.black);
                }

            }
        });

        // 设置文本标签和文本框存储名字
        JLabel namejLabel = new JLabel("验证码：");
        namejLabel.setBounds(120, 450, 110, 50);
        namejLabel.setFont(new Font("黑体", Font.BOLD, 25));
        jPanel.add(namejLabel, 0);
        passwordField = new JTextField();
        passwordField.setBounds(230, 450, 100, 50);
        passwordField.setFont(new Font("黑体", Font.BOLD, 20));
        passwordField.setForeground(Color.gray);
        jPanel.add(passwordField, 0);
        JLabel jLabel4 = new JLabel("",0);
        jLabel4.setBounds(230, 500, 200, 50);
        jLabel4.setForeground(Color.gray);
        jLabel4.setFont(new Font("黑体", Font.BOLD, 20));
        jPanel.add(jLabel4, 0);

        // 设置聚焦失焦事件
        passwordField.addFocusListener(new FocusListener() {

            @Override
            public void focusLost(FocusEvent e) {
                // TODO Auto-generated method stub
                if (!jButton.getText().equals("获取")){
                    if (passwordField.getText().equals(""+anInt)){
                        jLabel4.setText("验证码正确");
                        cd = true;
                    }else{
                        cd = false;
                        passwordField.setText("");
                        jLabel4.setText("验证码错误");
                    }
                }else{
                    cd = false;
                    passwordField.setText("");
                    jLabel4.setText("请获取验证码");
                }
            }
            @Override
            public void focusGained(FocusEvent e) {
                // TODO Auto-generated method stub
                if (!jButton.getText().equals("获取")){
                    jLabel4.setText("验证码五分钟内有效");
                }else{
                    jLabel4.setText("请获取验证码");
                }
            }
        });
        jButton = new JButton("获取");
        jButton.setBounds(330, 450, 100, 50);
        jButton.setFont(new Font("黑体",Font.BOLD,15));
        jPanel.add(jButton, 0);
        jButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                if(ab&&bc){
                    if (jButton.getText().equals("获取")){
                        Tool工具 tool工具 = new Tool工具();
                        tool工具.setSendSms(true);
                        SendSmsth sendSms = new SendSmsth();
                        sendSms.start();
                        anInt = tool工具.getSendSms();
                        tool工具.SendSms(nameField.getText(),anInt);
                    }else {
                        jLabel4.setText("正在获取验证码");
                    }
                }else{
                    jLabel4.setText("用户名或号码错误");
                }

            }
        });


        //设置密码和确认密码
        JLabel jLabel2 = new JLabel("新密码");
        jLabel2.setBounds(130,550,110, 50);
        jLabel2.setFont(new Font("黑体", Font.BOLD, 25));
        jPanel.add(jLabel2, 0);
        JLabel jLabel3 = new JLabel("确认密码");
        jLabel3.setBounds(110,650,150, 50);
        jLabel3.setFont(new Font("黑体", Font.BOLD, 25));
        jPanel.add(jLabel3, 0);
        JPasswordField jTextField = new JPasswordField();
        jTextField.setBounds(230, 550, 200, 50);
        jTextField.setForeground(Color.gray);
        jTextField.setFont(new Font("黑体", Font.BOLD, 20));
        jPanel.add(jTextField, 0);

        JLabel jLabel12 = new JLabel("",0);
        jLabel12.setBounds(230, 600, 200, 50);
        jLabel12.setFont(new Font("黑体", Font.BOLD, 20));
        jPanel.add(jLabel12, 0);
        // 设置聚焦失焦事件
        jTextField.addFocusListener(new FocusListener() {

            @Override
            public void focusLost(FocusEvent e) {
                // TODO Auto-generated method stub
                if (jTextField.getText().equals("")) {
                    jLabel12.setText("请设置你的密码");
                    jLabel12.setForeground(Color.gray);
                } else {
                    if (!jTextField.getText().matches("[0-9a-zA-Z]{6,12}")) {
                        jLabel12.setText("6-12(数字或字母)");
                    }else {
                        jLabel12.setText("符合条件");
                    }
                }
            }
            @Override
            public void focusGained(FocusEvent e) {
                // TODO Auto-generated method stub
                jLabel12.setText("6-12(数字或字母)");
                jLabel12.setForeground(Color.black);
            }
        });

        JPasswordField jTextField1 = new JPasswordField();
        jTextField1.setBounds(230, 650, 200, 50);
        jTextField1.setForeground(Color.gray);
        jTextField1.setFont(new Font("黑体", Font.BOLD, 20));
        jPanel.add(jTextField1, 0);

        JLabel jLabel13 = new JLabel("",0);
        jLabel13.setBounds(230, 700, 200, 50);
        jLabel13.setFont(new Font("黑体", Font.BOLD, 20));
        jPanel.add(jLabel13, 0);
        // 设置聚焦失焦事件
        jTextField1.addFocusListener(new FocusListener() {

            @Override
            public void focusLost(FocusEvent e) {
                // TODO Auto-generated method stub
                if (jTextField.getText().equals(jTextField1.getText())){
                    jLabel13.setText("密码正确");
                    jLabel13.setForeground(Color.green);
                    df=true;
                }else{
                    df=false;
                }
            }
            @Override
            public void focusGained(FocusEvent e) {
                // TODO Auto-generated method stub
                jLabel13.setText("请在一次输入密码");
                jLabel13.setForeground(Color.black);
            }
        });

        // 设置按钮
        registerButton = new JButton("返回登陆");
        registerButton.setBounds(130, 750, 110, 50);
        registerButton.setFont(new Font("黑体", Font.BOLD, 15));
        registerButton.setBackground(new Color(203, 232, 234));
        registerButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        // 设置点击事件
        registerButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                Tool工具 tool工具 = new Tool工具();
                tool工具.setSendSms(false);
                new Land(jFrame);
            }
        });
        jPanel.add(registerButton, 0);

        // 注册按钮
        landButton = new JButton("修改密码");
        landButton.setBounds(240, 750, 190, 50);
        landButton.setFont(new Font("黑体", Font.BOLD, 15));
        landButton.setBackground(new Color(203, 232, 234));
        landButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        jPanel.add(landButton, 0);

        // 书写点击事件
        landButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                if (landButton.getText().equals("修改密码")) {
                    if (ab && bc && cd && df) {
                        int a = userDao.updateUser(jTextField.getText(),userField.getText());
                        if (a>0){
                            JOptionPane.showMessageDialog(jFrame,"修改成功");
                            new Land(jFrame);
                            usList = userDao.queryStudents();
                        }else{
                            JOptionPane.showMessageDialog(jFrame, "修改密码", "修改错误", JOptionPane.ERROR_MESSAGE);
                        }
                    }
                }
            }
        });
        // 加载并刷新面板
        jFrame.repaint();
        jFrame.revalidate();
    }
    class SendSmsth extends Thread{

        public void run() {
            long l = new Date().getTime();
            Tool工具 tool工具 = new Tool工具();
            l+=5*60*1000;
            while (l!=new Date().getTime()&&tool工具.getsendSms()){
                if (l-new Date().getTime()<200){
                    jButton.setText("获取");
                }else{
                    jButton.setText((l-new Date().getTime())/1000+"");
                }

            }

        }
    }
}
